package com.kirk.crawler.core.launcher;

import com.kirk.crawler.core.constants.SystemConstants;
import com.kirk.crawler.core.context.CrawlerToolContextData;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 *  <p> SpringLauncherImpl.java </p>
 *
 * @Author      kirkliu
 * @Date        2020-08-29 22:22
 * @version     1.0.0
 * @Description ILauncher接口Spring实现类,负责加载Spring配置文件
 */
public class SpringLauncherImpl implements ILauncher {
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.javacoo.icrawler.launcher.ILauncher#launch()
	 */
	@Override
	public void launch() {
		Map<String, Map<String, String>> plugins = CrawlerToolContextData.getInstance().getPlugins();
		List<String> activePlugins = new ArrayList<>(10);
		activePlugins.add("resources/spring/applicationContext.xml");
		String key = "";
		String[] tempPaths = null;
		for(Entry<String, Map<String, String>> entry : plugins.entrySet()){
			if(Boolean.valueOf(entry.getValue().get(SystemConstants.PLUGIN_PROPERTIES_KY_ACTIVE))){
				if(StringUtils.isNotBlank(entry.getValue().get(SystemConstants.PLUGIN_PROPERTIES_KY_BEANXMLPATH))){
					tempPaths = entry.getValue().get(SystemConstants.PLUGIN_PROPERTIES_KY_BEANXMLPATH).split(",");
					for(String path : tempPaths){
						activePlugins.add(path);
					}
				}
			}
		}
		int i = 0;
		String[] contextPaths = new String[activePlugins.size()];
		for(String xmlPath : activePlugins){
			contextPaths[i] = xmlPath;
			i++;
		}
		CrawlerToolContextData.getInstance().setApplicationContext(new ClassPathXmlApplicationContext(contextPaths));
	}
	
    
}
